package com.zhu.controller.admin;

import com.zhu.base.http.RestResponse;
import com.zhu.domain.Goods;
import com.zhu.domain.TableColumn;
import com.zhu.domain.enums.PropTypeEnum;
import com.zhu.model.admin.GetAllGoodsVM;
import com.zhu.model.admin.TableColumnVM;
import com.zhu.model.goods.GoodsConditionSelectVM;
import com.zhu.model.goods.GoodsInsertVM;
import com.zhu.model.goods.GoodsSelectVM;
import com.zhu.model.goods.GoodsUpdateVM;
import com.zhu.service.GoodsService;
import com.zhu.service.InventoryService;
import com.zhu.service.TableColumnService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static java.util.UUID.randomUUID;

/**
 * @author JoyBoyZH
 * @description 管理员商品类控制层接口
 * @date 2022/6/3
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/gim/api/admin/manageGoods")
@Api(tags = "管理员商品模块说明")
public class AdminGoodsController {
    private final ModelMapper modelMapper;

    private final GoodsService goodsService;
    private final InventoryService inventoryService;

    private final TableColumnService tableColumnService;

    @Autowired
    public AdminGoodsController(ModelMapper modelMapper, GoodsService goodsService, InventoryService inventoryService, TableColumnService tableColumnService) {
        this.modelMapper = modelMapper;
        this.goodsService = goodsService;
        this.inventoryService = inventoryService;
        this.tableColumnService = tableColumnService;
    }

    @PostMapping(value = "/selectGoods")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("获取所有商品接口说明")
    public RestResponse<Object> selectGoods(@RequestBody GoodsConditionSelectVM models) {
        List<Goods> goodsList = goodsService.getGoodsList(models);
        if (goodsList.isEmpty()) {
            return RestResponse.failure("暂无货品");
        }
        List<GoodsSelectVM> goodsSelectList = new ArrayList<>();
        for (Goods goods : goodsList) {
            GoodsSelectVM modelInfo = modelMapper.map(goods, GoodsSelectVM.class);
            modelInfo.setNumber(inventoryService.getGoodsTotalNum(goods.getGoodsId()));
            goodsSelectList.add(modelInfo);
        }
        GetAllGoodsVM model = new GetAllGoodsVM();
        List<TableColumn> tableColumnList = tableColumnService.getList(PropTypeEnum.GOODS.getCode());
        List<TableColumnVM> tableColumnVMList = new ArrayList<>();
        for (TableColumn tc : tableColumnList) {
            TableColumnVM tableColumnVM = new TableColumnVM();
            tableColumnVM.setProp(tc.getProp());
            tableColumnVM.setPropName(tc.getPropName());
            tableColumnVMList.add(tableColumnVM);
        }
        model.setTableColumnList(tableColumnVMList);
        model.setGoodsSelectList(goodsSelectList);
        model.setTotalNum(goodsList.size());
        return RestResponse.ok(model);
    }

    @GetMapping(value = "/selectGoodsPrice")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("获取商品价格范围区间说明")
    public RestResponse<Object> selectGoodsPrice(){
        List<Goods> list = goodsService.getAllGoods();
        Integer[] amount = new Integer[]{0,0,0,0,0,0,0,0,0,0,0};
        for(Goods goods : list){
            if(goods.getPrice()/10>10){
                amount[10]+=1;
            }else{
                amount[(int) (goods.getPrice()/10)]+=1;
            }
        }
        return RestResponse.ok(amount);
    }

    @PostMapping(value = "/insertGoods")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("添加商品接口说明")
    public RestResponse<Object> insertGoods(@RequestBody GoodsInsertVM model) {
        Goods goods = modelMapper.map(model, Goods.class);
        goods.setGid("goods" + randomUUID().toString().substring(0,8));
        Goods goodsInfo = goodsService.getGoodsByCondition(model);
        if (goodsInfo != null) {
            return RestResponse.failure("信息重复");
        } else {
            goodsService.saveGoods(goods);
            return RestResponse.ok("添加成功");
        }
    }

    @GetMapping(value = "/deleteGoods/{goodsId}")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("删除商品接口说明")
    public RestResponse<Object> deleteGoods(@PathVariable Integer goodsId) {
        if (goodsService.deleteGoods(goodsId)) {
            return RestResponse.ok("删除成功");
        } else {
            return RestResponse.failure("删除失败");
        }
    }


    @PostMapping(value = "/updateGoods/{goodsId}")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("修改商品接口说明")
    public RestResponse<Object> updateGoods(@PathVariable Integer goodsId, @RequestBody GoodsUpdateVM model) {
        Goods goods = goodsService.getGoodsById(goodsId);
        goods.setGoodsName(model.getGoodsName());
        goods.setColor(model.getColor());
        goods.setSize(model.getSize());
        goods.setPrice(model.getPrice());
        GoodsInsertVM modelInfo = modelMapper.map(model, GoodsInsertVM.class);
        if (goodsService.getGoodsByCondition(modelInfo) != null) {
            return RestResponse.failure("信息重复,修改失败");
        }
        if (goodsService.updateGoods(goods)) {
            return RestResponse.ok("修改成功");
        } else {
            return RestResponse.failure("修改失败");
        }
    }

}
